#include <iostream>
#include <string>
#include <fstream>

using namespace std;
using std::string;
using std::fstream;

typedef struct WAV_HEADER{
    char    RIFF[4]; //RIFF Header  Magic Header
    unsigned long   ChunkSize;  // RIFF ChunkSize
    char    WAVE[4];    //WAV Header
    char    fmt[4];     //FMT Header
    unsigned long SubChunk1Size; //Size of the fmt chunk
    unsigned short AudioFormat; // Audio Format 1=PCM,6=mulaw,7=alaw
    unsigned short NumOfChan; //Number of channels 1=Mono, 2=Stereo
    unsigned long SamplesPerSec; //Sampling Frequency in Hz
    unsigned long bytesPerSec; //Bytes per second
    unsigned short blockAlign; // 2=16-bit mono, 4=16-bit stereo
    unsigned short bitsPerSample; //Number of bits per sample
    char    SubChunk2ID[4]; //"Data" string
    unsigned long SubChunk2Size; //Sampled data length
}wav_hdr;

// Function prototypes
int getFileSize(FILE *inFile);

int main(int argc, char *argv[])
{
    wav_hdr wavHeader;
    FILE *wavFile;
    int headerSize = sizeof(wav_hdr);
    int filelength = 0;

    string answer;

    do{
        string input;
        string answer;
        string path;

        const char* filePath;
        cout << "Enter the name of the WAV file:\n";
        cin >> input;
        cin.get();

        cout << endl;

        path = "/home/ph/" + input + ".wav";
        filePath = path.c_str();

        wavFile = fopen(filePath,"r");

        if(wavFile == NULL){ printf("Can not able to open wave file\n"); }

        fread(&wavHeader, headerSize,1,wavFile);
        filelength = getFileSize(wavFile);
        fclose(wavFile);

        cout << "File is                :" << filelength << "bytes." << endl;
        cout << "RIFF header            :" << wavHeader.RIFF[0]
                                           << wavHeader.RIFF[1] 
                                           << wavHeader.RIFF[2] 
                                           << wavHeader.RIFF[3] << endl;
        cout << "WAVE header            :" << wavHeader.WAVE[0]
                                           << wavHeader.WAVE[1]
                                           << wavHeader.WAVE[2]
                                           << wavHeader.WAVE[3] << endl;
        cout << "FMT                    :" << wavHeader.WAVE[0]
                                           << wavHeader.WAVE[1]
                                           << wavHeader.WAVE[2]
                                           << wavHeader.WAVE[3] << endl;
        cout << "Data size              :" << wavHeader.ChunkSize << endl;

        cout << "Sampling Rate          :" << wavHeader.SamplesPerSec << endl;
        cout << "Number of bits used    :" << wavHeader.bitsPerSample << endl;
        cout << "Number of channels     :" << wavHeader.NumOfChan << endl;
        cout << "Number of bytes per second :" << wavHeader.SubChunk2Size << endl;
        cout << "Audio Format           :" << wavHeader.AudioFormat << endl;

        cout << "Block align            :" << wavHeader.blockAlign << endl;

        cout << "Data string            :" << wavHeader.SubChunk2ID[0]
                                           << wavHeader.SubChunk2ID[1]
                                           << wavHeader.SubChunk2ID[2]
                                           << wavHeader.SubChunk2ID[3]
                                           << endl;
        cout << endl << endl << "Try something else? (y/n)";
        cin >> answer;
        cout << endl << endl;
    }while(answer == "y");

    getchar();
    return 0;
}

int getFileSize(FILE *inFile){
    int fileSize = 0;
    fseek(inFile,0,SEEK_END);

    fileSize=ftell(inFile);

    fseek(inFile,0,SEEK_SET);
    return fileSize;
}
        
